Imports System.Diagnostics.Process
Imports System.Threading
Public Class F_Food_Medicine
    Dim WithEvents SBO_Application As SAPbouiCOM.Application
    Dim Ocompany As SAPbobsCOM.Company
    Sub New(ByVal ocompany1 As SAPbobsCOM.Company, ByVal sbo_application1 As SAPbouiCOM.Application)
        SBO_Application = sbo_application1
        Ocompany = ocompany1
    End Sub
    Public Sub Food_bind(ByVal oform As SAPbouiCOM.Form)
        Try
            'CFL_BP_PetMaster(oform, SBO_Application)
            CFL_BP_PetMaster(oform, SBO_Application)
            CFL_BP_Employee(oform, SBO_Application)
            oEdit = oform.Items.Item("1000001").Specific
            oEdit.String = Format(Now.Date, "dd/MM/yy")
            oEdit = oform.Items.Item("6").Specific
            oEdit.String = Format(Now.Date, "dd/MM/yy")
            oEdit = oform.Items.Item("8").Specific
            oEdit.String = "Open"
            oEdit = oform.Items.Item("15").Specific
            oEdit.ChooseFromListUID = "CFLPM"
            oEdit.ChooseFromListAlias = "U_PetCode"
            'oEdit = oform.Items.Item("1000001").Specific
            'oEdit.ChooseFromListUID = "CFLEMP"
            'oEdit.ChooseFromListAlias = "empID"
            oform.DataBrowser.BrowseBy = "4"

        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Public Sub Food_bind_Report(ByVal oform As SAPbouiCOM.Form)
        Try
            CFL_BP_PetMaster(oform, SBO_Application)
            oform.DataSources.UserDataSources.Add("oedit1", SAPbouiCOM.BoDataType.dt_SHORT_TEXT)
            oform.DataSources.UserDataSources.Add("oedit2", SAPbouiCOM.BoDataType.dt_SHORT_TEXT)
            oform.DataSources.UserDataSources.Add("oedit3", SAPbouiCOM.BoDataType.dt_DATE)
            oform.DataSources.UserDataSources.Add("oedit4", SAPbouiCOM.BoDataType.dt_DATE)
            oform.DataSources.UserDataSources.Add("oedit5", SAPbouiCOM.BoDataType.dt_SHORT_TEXT)
            oEdit = oform.Items.Item("4").Specific
            oEdit.DataBind.SetBound(True, "", "oedit1")
            oEdit.ChooseFromListUID = "CFLPM"
            oEdit.ChooseFromListAlias = "U_PetCode"
            oEdit = oform.Items.Item("8").Specific
            oEdit.DataBind.SetBound(True, "", "oedit3")
            oEdit.String = Format(Now.Date, "dd/MM/yy")
            oEdit = oform.Items.Item("10").Specific
            oEdit.DataBind.SetBound(True, "", "oedit4")
            oEdit.String = Format(Now.Date, "dd/MM/yy")
            oCombo = oform.Items.Item("12").Specific
            oCombo.DataBind.SetBound(True, "", "oedit5")
            oCombo.ValidValues.Add("All", "A")
            oCombo.ValidValues.Add("Food", "F")
            oCombo.ValidValues.Add("Medicine", "M")
            oCombo.Select("A", SAPbouiCOM.BoSearchKey.psk_ByDescription)

        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Private Sub SBO_Application_ItemEvent(ByVal FormUID As String, ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.ItemEvent
        Try

            If pVal.FormUID = "FoodMedicine" And pVal.ItemUID = "Print" And pVal.Before_Action = True And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED Then
                'Food_Medicine_Report
                Dim trd As Threading.Thread
                trd = New Threading.Thread(AddressOf Food_Medicine_Report)
                trd.IsBackground = True
                trd.SetApartmentState(ApartmentState.STA)
                trd.Start()
            End If
            '----------------
            If pVal.FormUID = "FoodMedicine" Then
                oForm = SBO_Application.Forms.Item("FoodMedicine")
                If pVal.EventType = SAPbouiCOM.BoEventTypes.et_CHOOSE_FROM_LIST Then
                    Dim oCFLEvento As SAPbouiCOM.IChooseFromListEvent
                    oCFLEvento = pVal
                    Dim sCFL_ID As String
                    sCFL_ID = oCFLEvento.ChooseFromListUID
                    oForm = SBO_Application.Forms.Item(FormUID)
                    Dim oCFL As SAPbouiCOM.ChooseFromList
                    oCFL = oForm.ChooseFromLists.Item(sCFL_ID)
                    Try
                        If oCFLEvento.BeforeAction = False Then
                            Dim oDataTable As SAPbouiCOM.DataTable
                            oDataTable = oCFLEvento.SelectedObjects
                            If pVal.ItemUID = "4" Then

                                Try
                                    oEdit = oForm.Items.Item("4").Specific
                                    oEdit.String = oDataTable.GetValue("U_PetCode", 0)
                                Catch ex As Exception
                                End Try
                            End If
                            If pVal.ItemUID = "6" Then
                                'oEdit = oForm.Items.Item("26").Specific
                                'oEdit.String = oDataTable.GetValue("U_U_PetName", 0)
                                Try
                                    oEdit = oForm.Items.Item("6").Specific
                                    oEdit.String = oDataTable.GetValue("U_PetCode", 0)
                                Catch ex As Exception
                                End Try

                            End If
                        End If
                    Catch ex As Exception
                        '' SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                    End Try
                End If
            End If

            '------------------
            Try
                If pVal.FormUID = "AI_FOODMED" Then
                    oForm = SBO_Application.Forms.Item("AI_FOODMED")
                    If pVal.EventType = SAPbouiCOM.BoEventTypes.et_CHOOSE_FROM_LIST Then
                        Dim oCFLEvento As SAPbouiCOM.IChooseFromListEvent
                        oCFLEvento = pVal
                        Dim sCFL_ID As String
                        sCFL_ID = oCFLEvento.ChooseFromListUID
                        oForm = SBO_Application.Forms.Item(FormUID)
                        Dim oCFL As SAPbouiCOM.ChooseFromList
                        oCFL = oForm.ChooseFromLists.Item(sCFL_ID)
                        Try
                            If oCFLEvento.BeforeAction = False Then
                                Dim oDataTable As SAPbouiCOM.DataTable
                                oDataTable = oCFLEvento.SelectedObjects
                                If pVal.ItemUID = "15" Then
                                    oEdit = oForm.Items.Item("17").Specific
                                    oEdit.String = oDataTable.GetValue("U_PetName", 0)
                                    Try
                                        oEdit = oForm.Items.Item("15").Specific
                                        oEdit.String = oDataTable.GetValue("U_PetCode", 0)
                                    Catch ex As Exception
                                    End Try
                                End If

                            End If
                        Catch ex As Exception
                            '' SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                        End Try
                    End If
                End If
                '--------------------
            Catch ex As Exception
            End Try
            '======================================================

            Try
                '-------------------End Choose From List-----------
                If pVal.FormUID = "AI_FOODMED" And pVal.InnerEvent = False Then
                    oForm = SBO_Application.Forms.Item("AI_FOODMED")
                    If pVal.Before_Action = False Then
                        Select Case pVal.EventType
                            Case SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED
                                If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then
                                    If pVal.ItemUID = "1" Then
                                        oEdit = oForm.Items.Item("1000001").Specific
                                        oEdit.String = Format(Now.Date, "dd/MM/yy")
                                        oEdit = oForm.Items.Item("6").Specific
                                        oEdit.String = Format(Now.Date, "dd/MM/yy")
                                        oEdit = oForm.Items.Item("8").Specific
                                        oEdit.String = "Open"

                                    End If
                                End If

                            Case SAPbouiCOM.BoEventTypes.et_COMBO_SELECT


                        End Select
                    ElseIf pVal.Before_Action = True Then
                        Select Case pVal.EventType
                            Case SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED
                                If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE Or pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then

                                    If pVal.ItemUID = "1" Then
                                        oEdit = oForm.Items.Item("15").Specific
                                        If oEdit.String = "" Then
                                            SBO_Application.StatusBar.SetText("Select Pet Code", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If
                                        oEdit = oForm.Items.Item("1000001").Specific
                                        If oEdit.String = "" Then
                                            SBO_Application.StatusBar.SetText("Select Attendance Date", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If
                                        oEdit = oForm.Items.Item("6").Specific
                                        If oEdit.String = "" Then
                                            SBO_Application.StatusBar.SetText("Select Document Date", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                                            BubbleEvent = False
                                            Exit Sub
                                        End If

                                    End If
                                End If
                        End Select
                    End If
                End If
            Catch ex As Exception
                '   SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
            End Try

        Catch ex As Exception

        End Try

    End Sub
    Public Sub Food_Medicine_Report()
        oForm = SBO_Application.Forms.Item("FoodMedicine")
        Dim Sqlstr As String = ""
        Dim FPet As String = ""
        Dim TPet As String = ""
        Dim FDt As String = ""
        Dim TDt As String = ""
        Dim Type As String = ""
        oEdit = oForm.Items.Item("4").Specific
        If oEdit.String = "" Then
            FPet = "%"
        Else
            FPet = oEdit.String
        End If


        oEdit = oForm.Items.Item("8").Specific
        Dim yr As String
        If oEdit.String = "" Then
            yr = "2000"
            FDt = yr & "-01-01"
        Else
            FDt = oEdit.Value
        End If

        oEdit = oForm.Items.Item("10").Specific
        If oEdit.String = "" Then
            yr = Now.Year.ToString
            TDt = yr & "-12-31"
        Else
            TDt = oEdit.Value
        End If
        Try
            oCombo = oForm.Items.Item("12").Specific
            Type = oCombo.Selected.Value
            If Type = "All" Then
                Type = "%"
            End If
        Catch ex As Exception

        End Try

        Try
            SBO_Application.StatusBar.SetText("Retrieving Data", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success)
            'Sqlstr = "SELECT T0.[U_AI_PetCode], T0.[U_AI_PetName], T0.[U_AI_Date], T0.[U_AI_Type], T0.[U_AI_ItemDesc], T0.[U_AI_Expiry], T0.[U_AI_Comments] FROM [dbo].[@AI_FOODMED]  T0 WHERE T0.[U_AI_PetCode]  like '" & FPet & "'  and  T0.[U_AI_Date]  between '" & FDt & "' and '" & TDt & "' and  T0.[U_AI_Type]  like '" & Type & "' ORDER BY T0.[U_AI_Date],T0.[U_AI_PetName]"
            Sqlstr = "SELECT T0.[U_AI_PetCode], T0.[U_AI_PetName], T0.[U_AI_Date], T0.[U_AI_Type], T0.[U_AI_ItemDesc], T0.[U_AI_Expiry], T0.[U_AI_Comments] FROM [dbo].[@AI_FOODMED]  T0 WHERE T0.[U_AI_PetCode]  like '" & FPet & "'  and  T0.[U_AI_Date]  between '" & FDt & "' and '" & TDt & "' and  T0.[U_AI_Type]  like '" & Type & "' Union SELECT T0.[U_AI_PetCode], T0.[U_AI_PetName], T0.[U_AI_Date], T0.[U_AI_Type], T0.[U_AI_ItemDesc], T0.[U_AI_Expiry], T0.[U_AI_Comments] FROM [dbo].[@AI_FOODMED]  T0 WHERE T0.[U_AI_PetCode]  like '" & FPet & "'  and  isnull(T0.[U_Show],'')='Y'  and  T0.[U_AI_Type]  like '" & Type & "' ORDER BY T0.[U_AI_Date],T0.[U_AI_PetName]"
            Dim frm As MY_Report
            frm = New MY_Report
            frm.Food_Report_Report(Sqlstr, Ocompany)
            frm.Text = "Food and Medicine"
            frm.TopMost = True
            frm.Activate()
            frm.ShowDialog()
        Catch ex As Exception
        End Try

    End Sub
End Class
